AiCommanderName  = "Storm Kommander AI";
_aiSide = _this select 0;


Side0SpecialIC = 0;
Side1SpecialIC = 0;

if (_aiSide == side0) then
{
	friendlybase = side0base; enemybase = side1base; aiMakerColor = side0_MarkerColor;

};
if (_aiSide == side1) then
{
	friendlybase = side1base; enemybase = side0base; aiMakerColor = side1_MarkerColor;
};

PreferTanks = false;
PreferAir = false;
PreferInf = false;


Side0AssBought = false;
Side1AssBought = false;

Side0Bought = false;
Side1Bought = false;
assNeutral = false;

[] execVM "server\coop\commanders\granQ_Reset.sqf";

waituntil  {count aiStrategicPoints > 2};
action_neutral = false;
action_assault = false;
action_defensive = false;
BuiltSpecial = false;

while { true } do
{
	
	_aiStrategicPoints = aiStrategicPoints;
	_aiDefPoints = [];
	aiEnemyLocations = [];
	aiNeutralPoints = [];

	/*
		Old versions supported ai commander on both sides, so this is temporary
	*/

	if(_aiSide == side0 ) then {ai_money_income = side0_money_income; ai_upkeep = side0_upkeep};
	if(_aiSide == side1 ) then {ai_money_income = side1_money_income; ai_upkeep = side1_upkeep};


	/* 
		First we sort the victory locations into enemy, friendly or neutral if neither side got them.
		Also find out which are "bases" also known as FOB (Forward operating bases)

		To be included is to sort _aiStrategicPoints after the value of the control point.
	*/
	{
		_logic = _x select 0;
		_marker = _x select 2;
		_AiValue = _x select 3;

		if ((markerColor _marker == side0_MarkerColor) And (_aiSide == side0)) then { _aiDefPoints = _aiDefPoints + [_logic];};
		if ((markerColor _marker == side1_MarkerColor) And (_aiSide == side1)) then { _aiDefPoints = _aiDefPoints + [_logic];};

		if ((markerColor _marker == side1_MarkerColor) And (_aiSide == side0)) then {aiEnemyLocations = aiEnemyLocations + [_logic];};
		if ((markerColor _marker == side0_MarkerColor) And (_aiSide == side1)) then {aiEnemyLocations = aiEnemyLocations + [_logic];};

		if (markerColor _marker == side2_MarkerColor) then {aiNeutralPoints = aiNeutralPoints + [[_logic,_aiValue,_marker]];};
		sleep 0.1;

	} foreach _aiStrategicPoints;




	/* 
		AI Threat assestment
	*/

	if (EnemyThreatTanks > EnemyThreatAir AND EnemyThreatTanks > EnemyThreatInf) then { PreferTanks = true; PreferAir = false; PreferInf = false; };
	if (EnemyThreatAir > EnemyThreatTanks AND EnemyThreatAir > EnemyThreatInf) then { PreferTanks = false; PreferAir = true; PreferInf = false;};
	if (EnemyThreatInf > EnemyThreatTanks ) then { PreferTanks = false; PreferAir = false; PreferInf = true;};
	





	/*
		Decide which actions to take on this run.
		Neutral flags are of highest priority
	*/
	
	// aiNeutralPoints
	if ((count aiNeutralPoints > 0) AND !(action_neutral)) then
	{
		/*
			Now its time to sort them after their value
		*/
		aiNeutralPoints = [aiNeutralPoints,1] call fnc_sortNestedArray;

		_aiNeutralSorted = [];
		{ _aiNeutralSorted = [[(_x select 0),_x select 2]] + _aiNeutralSorted; } foreach aiNeutralPoints;
		action_neutral = true;
		_wait = 0;
		{ _wait = _wait + 7; [_x select 0,_aiSide,_wait, _x select 1] execVM "server\coop\commanders\granQ_selectUnit\assaultNeutral.sqf"; sleep 0.1;} foreach _aiNeutralSorted;
	};



	if ((count aiEnemyLocations > 0) AND !(action_assault)) then
	{
		/*
			Two options available
				Attack the enemy location closet to AI's base.  (33 %)
				Attack the enemy location closet to a forward operation base. (66 %)
		*/
		if (random 10 > 7) then
		{
			//	Attack the enemy location closet to AI's base.

			assault_position = [friendlybase,aiEnemyLocations] call fGetNearestArray;
		} else
		{
			//	Attack the enemy location closet to a forward operation base.
			_base = [getpos enemybase, _aiSide] call fGetNearestFOB;
			sleep 1;
			assault_position = [_base,aiEnemyLocations] call fGetNearestArray;
		};

		// Launch the attack and move the debug marker.

		[assault_position,_aiSide] execVM "server\coop\commanders\granQ_selectunit\assault.sqf";

		"ai_Assault" setmarkerpos [getpos assault_position select 0, getpos assault_position select 1];


		if(_aiSide == side0) then {Side0AssultPostion = getpos assault_position;};
		if(_aiSide == side1) then {Side1AssultPostion = getpos assault_position;};


		// This is to launch a second attack if we got a good economy and some targets.
		// This is a rare, stronger attack that goes deeper into the enemy area.

		aiEnemyLocations = aiEnemyLocations - [assault_position];

		_nr = count aiEnemyLocations;
		if ((_aiSide == side0 ) AND (side0_upkeep < side0_money_income) AND (_nr > 0) AND !(BuiltSpecial)) then {
			assault_positionSecond = [friendlybase,aiEnemyLocations] call fGetNearestArray;
			[assault_positionSecond,_aiSide,_aiStrategicPoints] execVM "server\coop\commanders\granQ_selectunit\special.sqf";
		};
		if ((_aiSide == side1 ) AND (side1_upkeep < side1_money_income) AND (_nr > 0) AND !(BuiltSpecial)) then
		{
			 assault_positionSecond = [friendlybase,aiEnemyLocations] call fGetNearestArray;
			[assault_positionSecond,_aiSide,_aiStrategicPoints] execVM "server\coop\commanders\granQ_selectunit\special.sqf";
			BuiltSpecial = true;
		};
	};
	if ((count _aiDefPoints > 0) AND !(action_defensive) AND (ai_money_income > ai_upkeep)) then
	{
		action_defensive = true;
		_nr = count _aiDefPoints;

		if (_aiSide == side0 ) then { aiSide0DefPoints = _aiDefPoints;};
		if (_aiSide == side1 ) then { aiSide1DefPoints = _aiDefPoints;};

		_nr = _nr - 1;
		_i = Round(random _nr);
		_DefObj = [enemybase,_aiDefPoints] call fGetNearestArray;
		if(random 10 > 7) then {_DefObj = friendlybase; };
		"ai_Defend" setmarkerpos [getpos _defobj select 0, getpos _defobj select 1];
		[_DefObj,_aiSide,_aiStrategicPoints] execVM "server\coop\commanders\granQ_selectUnit\defend.sqf";
		action_defensive = false;
	};
	sleep 2;
};
/*


#DecisionsBuild

#DecisionsPostions
_i = -1
_nr = count _aiStrategicPoints - 1
if(_nr == -1) then goto "SkipPos"


_Allunits = list servertrigger
_GroupArray = []
{if (side _x == _aiSide) then {_GroupArray = _GroupArray + [_X]}} foreach _Allunits
_groups = [_GroupArray] call fgetGroups

if_groups
if(_aiSide == side0 ) AND (side0_money < 0) then {_x move Side0AssultPostion} foreach _groups
if(_aiSide == side1 ) AND (side1_money < 0) then {_x move Side1AssultPostion} foreach _groups
if(count _groups > 140) then goto "skipdef"



*/